import _ from 'underscore';
export default {
  N_298: 298,
  N_299: 299,
  N_300: 300,
  N_301: 301,
  N_302: 302,
  N_303: 303,
  N_304: 304,
  N_305: 305,
  N_306: 306,
  solve_type_308: 308,
  solve_type_309: 309,
  sub_flag_no: 383,
  sub_flag_have: 383,
  returnArr(length) {
    var ArrayObj = new Array(); // 创建数组对象
    for (var i = 0; i < length; i++) {
      ArrayObj[i] = new Array(); // 在每一个数组元素内再定义一个数组
    }
    return ArrayObj;
  },
  initAnswer(resData, _obj) {
    let res = [];
    resData.forEach((item) => {
      if (
        item.question_type == this.N_303 ||
        item.question_type == this.N_304 ||
        item.question_type == this.N_305
      ) {
        let new_event = Object.assign({}, item.pub_answer, {
          sub_state: _.pluck(item.subQuestionList, 'pub_answer'),
        });
        res.push(new_event);
      } else {
        res.push(item.pub_answer);
      }
    });
    return res;
  },

  // returnBeforeAnswer (index_2) {
  //   return {
  //     list: JSON.parse(window.localStorage.getItem('BEFORE_QUESTION_LIST')),
  //     obj: JSON.parse(window.localStorage.getItem('BEFORE_QUESTION_LIST'))[index_2]
  //   }
  // },
  // setbeforeanswer (list) {
  //   window.localStorage.setItem('BEFORE_QUESTION_LIST', JSON.stringify(list))
  // },
  // 返回课堂练数据模型
  returnC(resData, param) {
    let _s = [];
    resData.forEach((item) => {
      if (
        item.question_type == this.N_298 ||
        item.question_type == this.N_299 ||
        item.question_type == this.N_300
      ) {
        if (item.solution) {
          item.solution_json = JSON.parse(item.solution);
          item.solution_arr = item.solution_json.html || '';
          item.user_answer = [{ index: 0, content: '' }];
          if (item.question_type == this.N_298) {
            item.chooseanswer_arr.forEach((i, index) => {
              i.addAnswer = i.value == item.solution_arr;
            });
          } else if (item.question_type == this.N_299) {
            item.chooseanswer_arr.forEach((i, index) => {
              i.addAnswer = item.solution_arr.indexOf(i.value) != -1;
            });
          } else if (item.question_type == this.N_300) {
          }
        }
      }
      // 填空题
      if (
        item.question_type == this.N_301 ||
        item.question_type == this.N_302
      ) {
        item.solution_json = JSON.parse(item.solution);
        item.solution_arr = item.solution_json.html || '';
        item.user_answer = item.solution_json.html;
        if (Array.isArray(item.user_answer)) {
          item.user_answer.forEach((item, index) => {
            item.content = '';
          });
        }
      }
      let answerinfo = [];
      let event_obj = {
        homework_id: param.homework_id,
        homework_question_id: item.id,
        answer: '',
        answer_type: item.question_type,
        user_id: param.user_id,
      };
      // 填空  简答题
      if (
        item.question_type == this.N_301 ||
        item.question_type == this.N_302
      ) {
        item.solution_arr.forEach((item, index) => {
          event_obj = { ...event_obj, idx: Number(index + 1) };
          answerinfo.push(event_obj);
        });
      }
      // 单选 多选 判断
      if (
        item.question_type == this.N_298 ||
        item.question_type == this.N_299 ||
        item.question_type == this.N_300
      ) {
        event_obj = { ...event_obj, idx: 1 };
        answerinfo.push(event_obj);
      }
      // 完形填空
      if (item.question_type == this.N_306) {
        answerinfo.push(event_obj);
      }
      item.pub_answer = {
        homework_id: param.homework_id,
        answer_type: item.question_type,
        homework_question_id: item.id,
        user_id: param.user_id,
        answerinfo: answerinfo,
      };
      if (
        item.question_type == this.N_303 ||
        item.question_type == this.N_304 ||
        item.question_type == this.N_305
      ) {
        this.returnData(item.subQuestionList, param);
      }
    });
    return resData;
  },
  returnData(resData, isAnswer, _obj) {
    resData.forEach((item) => {
      if (item.body) {
        item.body_json = JSON.parse(item.body);
        item.questionContent_arr = item.body_json.questionContent || [];
        item.chooseanswer_arr = item.body_json.chooseanswer || [];
        if (item.question_type == this.N_299) {
          item.body_json.chooseanswer_more = item.body_json.chooseanswer;
          item.body_json.chooseanswer_more.forEach((item, index) => {
            item.status = false;
          });
        }
      }
      if (
        item.question_type == this.N_298 ||
        item.question_type == this.N_299 ||
        item.question_type == this.N_300
      ) {
        if (item.solution) {
          item.solution_json = JSON.parse(item.solution);
          item.solution_arr = item.solution_json.html || '';
          item.user_answer = [{ index: 0, content: '' }];
          if (item.question_type == this.N_298) {
            item.chooseanswer_arr.forEach((i, index) => {
              i.addAnswer = i.value == item.solution_arr;
            });
          } else if (item.question_type == this.N_299) {
            item.chooseanswer_arr.forEach((i, index) => {
              i.addAnswer = item.solution_arr.indexOf(i.value) != -1;
            });
          } else if (item.question_type == this.N_300) {
          }
        }
      }
      // 填空题
      if (
        item.question_type == this.N_301 ||
        item.question_type == this.N_302
      ) {
        item.solution_json = JSON.parse(item.solution);
        item.solution_arr = item.solution_json.html || '';
        item.user_answer = item.solution_json.html;
        if (Array.isArray(item.user_answer)) {
          item.user_answer.forEach((item, index) => {
            item.content = '';
          });
        }
        // 键盘模式  简答题 替换
        if (
          item.question_type == this.N_302 &&
          item.solve_type == this.solve_type_308
        ) {
          let _source_body = JSON.parse(item.source_body).html; // 源题干的字符串
          let _sort = 0;
          let res = _source_body.replace(/##(.*?)##/g, (match, capture) => {
            _sort++;
            var newStr = `<input class='input_answer_an  input_answer_an${_sort}' placeholder="请输入答案" ></input>`;
            return newStr;
          });
          item._source_body = res;
        }
      }
      if (isAnswer) {
        let answerinfo = [];
        let event_obj = {
          homework_id: _obj.homework_id,
          homework_state_id: _obj.homework_state_id,
          answer_type: item.question_type,
          homework_question_id: item.question_type,
          solve_type: item.solve_type,
          answer: '',
          user_id: _obj.user_id,
          handinput_detail: [],
        };
        // 填空  简答题
        if (
          item.question_type == this.N_301 ||
          item.question_type == this.N_302
        ) {
          item.solution_arr.forEach((item, index) => {
            event_obj = { ...event_obj, idx: Number(index + 1) };
            answerinfo.push(event_obj);
          });
        }
        // 单选 多选 判断
        if (
          item.question_type == this.N_298 ||
          item.question_type == this.N_299 ||
          item.question_type == this.N_300
        ) {
          answerinfo.push(event_obj);
        }
        // 完形填空
        if (item.question_type == this.N_306) {
          answerinfo.push(event_obj);
        }
        item.pub_answer = {
          homework_id: _obj.homework_id,
          homework_state_id: _obj.homework_state_id,
          homework_question_id: item.id,
          answer_type: item.question_type,
          user_id: _obj.user_id,
          answerinfo: answerinfo,
        };
      }
      if (
        item.question_type == this.N_303 ||
        item.question_type == this.N_304 ||
        item.question_type == this.N_305
      ) {
        if (isAnswer) {
          this.returnData(item.subQuestionList, true, _obj);
        } else {
          this.returnData(item.subQuestionList);
        }
      }
    });
    return resData;
  },
  // _for (resARR, data) {
  //   resARR.forEach((item, index) => {
  //     if (item.sys_msg.type == 'Doodle_Type_Eraser') {
  //       let indexclear = item.sys_msg.index
  //       data.splice(indexclear, 1)
  //       data.splice(index - 1, 1)
  //       this._for(resARR, data)
  //     }
  //   })
  // },
  returnanswer(type, index) {
    let checkedcurrentList_answer =
      type == 'stl'
        ? JSON.parse(window.localStorage.getItem('checkedcurrentList_answer'))
        : JSON.parse(
            window.localStorage.getItem('checkedoutdoorTestList_answer')
          );
    let obj = checkedcurrentList_answer[index];
    return {
      checkedcurrentList_answer,
      obj,
    };
  },
};
